package com.andy.blog.admin.controller;

import com.andy.blog.admin.service.CacheBusService;
import com.andy.blog.common.reponse.GeneralResponseResult;
import com.andy.blog.dal.domain.SysCache;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 缓存监控
 * Created by andy on 2023-04-05 11:38
 */
@RestController
@RequestMapping("/monitor/cache")
public class CacheController {

    @Resource
    private CacheBusService cacheBusService;

    /**
     * 缓存监控信息
     */
    @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
    @GetMapping()
    public GeneralResponseResult<Map<String, Object>> getInfo() {
        return GeneralResponseResult.success(cacheBusService.getInfo());
    }


    @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
    @GetMapping("/getNames")
    public GeneralResponseResult<List<SysCache>> cache() {
        return GeneralResponseResult.success(cacheBusService.cache());
    }

    @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
    @GetMapping("/getKeys/{cacheName}")
    public GeneralResponseResult<Set<String>> getCacheKeys(@PathVariable String cacheName) {
        return GeneralResponseResult.success(cacheBusService.getCacheKeys(cacheName));
    }

    @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
    @GetMapping("/getValue/{cacheName}/{cacheKey}")
    public GeneralResponseResult<SysCache> getCacheValue(@PathVariable String cacheName, @PathVariable String cacheKey) {
        return GeneralResponseResult.success(cacheBusService.getCacheValue(cacheName, cacheKey));
    }

    @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
    @DeleteMapping("/clearCacheName/{cacheName}")
    public GeneralResponseResult<Void> clearCacheName(@PathVariable String cacheName) {
        cacheBusService.clearCacheName(cacheName);
        return GeneralResponseResult.success();
    }

    @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
    @DeleteMapping("/clearCacheKey/{cacheKey}")
    public GeneralResponseResult<Void> clearCacheKey(@PathVariable String cacheKey) {
        cacheBusService.clearCacheKey(cacheKey);
        return GeneralResponseResult.success();
    }

    @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
    @DeleteMapping("/clearCacheAll")
    public GeneralResponseResult<Void> clearCacheAll() {
        cacheBusService.clearCacheAll();
        return GeneralResponseResult.success();
    }
}
